package proto

import (
	"regexp"
	"sort"
	"strconv"
	"strings"
	D "websocket_apn/define"
	"websocket_apn/log"
)

/**
进入妖盟乱斗
[DEBUG] [2024/05/13 14:14:50] [src/websocket_apn/main_test.go]:[  193]:   MessageID: ↑↑↑↑↑ Client.351
[DEBUG] [2024/05/13 14:14:50] [src/websocket_apn/main_test.go]:[  197]:   S_UNION_BATTLE_ENTER - id:3109673861602
[DEBUG] [2024/05/13 14:14:50] [src/websocket_apn/main_test.go]:[  198]:   PayloadBytes: 71 AB 00 00 00 17 00 03 21 F5 00 00 02 D4 07 01 81 E2 08 92 EB CD 04
乱斗情况 攻击11次 排行3 202732
[DEBUG] [2024/05/13 14:14:50] [src/websocket_apn/main_test.go]:[  195]:   MessageID: ↓↓↓↓↓ Server.352
[DEBUG] [2024/05/13 14:14:50] [src/websocket_apn/main_test.go]:[  197]:   game cmd[5301]-unkown - id:3109673861602
[DEBUG] [2024/05/13 14:14:50] [src/websocket_apn/main_test.go]:[  198]:   PayloadBytes: 71 AB 00 00 00 3E 00 00 14 B5 00 00 02D4 07 01 81 E2 08 00 12 28 08 F8 B0 0C 10 03 18 00 20 C6 EA 9B 81 F7 31 30 04 38 E1 EC 89 EC 73 40 C2 4E 40 C3 4E 48 4A 50 A6 BD 0C 58 E9 01 60 02
08 00 12 28 08 F8 B0 0C 10 03 18 00 20 C6 EA 9B 81 F7 31 30 04 38 E1 EC 89 EC 73 40 C2 4E 40 C3 4E 48 4A 50 A6 BD 0C 58 E9 01 60 02
08 00 12 35 08 B4 F5 17 10 10 18 00 20 95 AA D6 B8 F7 31 30 01 38 E1 EC 89 EC 73 40 C2 4E 40 C3 4E 40 C4 4E 40 C5 4E 40 C6 4E 40 C7 4E 48 94 02 50 95 F9 17 58 C2 03 60 02
08 00 12 27 08 A1 C7 FF FF FF FF FF FF FF 01 10 D2 11 18 03 20 E5 85 B0 90 F7 31 30 00 38 BA C7 9A EC 73 48 0C 50 00 58 00 60 03

匹配
[DEBUG] [2024/05/13 21:42:09] [PRJ/websocket_apn/main_test.go]:[  193]:   MessageID: ↑↑↑↑↑ Client.6450
[DEBUG] [2024/05/13 21:42:09] [PRJ/websocket_apn/main_test.go]:[  197]:   S_UNION_BATTLE_MATCH - id:3109700251759
[DEBUG] [2024/05/13 21:42:09] [PRJ/websocket_apn/main_test.go]:[  198]:   PayloadBytes: 71 AB 00 00 00 1B 00 03 21 F7 00 00 02D4 08 94 30 6F 08 97 EB CD 04 10 00 18 00
[DEBUG] [2024/05/13 21:42:09] [PRJ/websocket_apn/main_test.go]:[  195]:   MessageID: ↓↓↓↓↓ Server.6451
[DEBUG] [2024/05/13 21:42:09] [PRJ/websocket_apn/main_test.go]:[  197]:   game cmd[1015]-unkown - id:3109700251759
[DEBUG] [2024/05/13 21:42:09] [PRJ/websocket_apn/main_test.go]:[  198]:   PayloadBytes: 71 AB 00 00 00 23 00 00 03 F7 00 00 02D4 08 94 30 6F 0A 0F 08 97 EB CD 04 12 08 08 B5 B4 06 10 64 18 64
[DEBUG] [2024/05/13 21:42:09] [PRJ/websocket_apn/main_test.go]:[  200]:   	>>> 1 - code 0A[10] len:15-08 97 EBCD 04 12 08 08 B5 B4 06 10 64 18 64_
�����dd
[DEBUG] [2024/05/13 21:42:09] [PRJ/websocket_apn/main_test.go]:[  195]:   MessageID: ↓↓↓↓↓ Server.6452
[DEBUG] [2024/05/13 21:42:09] [PRJ/websocket_apn/main_test.go]:[  197]:   game cmd[5303]-unkown - id:3109700251759
[DEBUG] [2024/05/13 21:42:09] [PRJ/websocket_apn/main_test.go]:[  198]:   PayloadBytes: 71 AB 00 00 01 D3 00 00 14 B7 00 00 02D4 08 94 30 6F 08 00 12 AF 03 08 99 C4 FF FF FF FF FF FF FF 01 10 D7 11 18 02 20 9C EC C1 91 F7 31 2A 85 03 08 F3 D3 0F 10 F4 03 18 00 22 D0 01 08 18 12 21 08 FF DA 97 EC 73 10 02 1A 12 E7 88 B1 E7 BB BF E8 89 B2 E7 9A 84 E6 9D BF E6 A0 9720 BF E6 BD 01 1A 3C 08 BD B1 A7 C2 C0 5A 12 12 E4 B9 90 E6 AD A3 E9 A3 9E E4 BA 8C E5 A4 A7 E7 8E 8B 18 A0 01 22 08 31 32 36 35 32 38 33 38 28 AA E5 08 30 94 4E 38 BF E6 BD 01 40 D1 FA 06 48 00 50 00 1A 36 08 BC B5 B4 C2 C0 5A 12 0C E8 949A E5 9C B0 E5 A4 A7 E4 BB 99 18 A5 01 22 08 31 33 39 34 38 39 36 37 28 9F E5 08 30 94 4E 38 BF E6 BD 01 40 D1 FA 06 48 00 50 00 1A 33 08 81 BA B1 C2 C0 5A 12 0C E4 BD 98 E8 82 86 E5 B0 8A E8 80 85 18 2E 22 06 37 34 38 31 37 39 28 AA E5 0830 91 4E 38 BF E6 BD 01 40 D3 FA 06 48 00 50 00 28 C7 9E C8 01 3A 06 08 01 10 F9 90 02 3A 06 08 02 10 F3 D3 0F 3A 05 08 03 10 A8 2E 3A 05 08 04 10 B9 11 3A 05 08 05 10 D9 01 3A 05 08 06 10 E4 02 3A 05 08 07 10 DE 03 3A 05 08 08 10 DD02 3A 05 08 09 10 95 02 3A 05 08 0A 10 8D 01 3A 05 08 0B 10 8F 03 3A 05 08 0C 10 C6 02 3A 05 08 0D 10 AD 03 3A 05 08 0E 10 88 05 3A 05 08 0F 10 BB 02 3A 05 08 10 10 EF 02 3A 04 08 17 10 3C 3A 04 08 19 10 0F 3A 04 08 1A 10 0B 3A 04 08 1B 100B 40 FF FF FF FF FF FF FF FF FF 01 50 00 58 AA E5 08 60 93 4E 68 D1 FA 06 70 00 30 00 38 BA C7 9A EC 73 48 0F 50 00 58 00 60 03 22 0B 0A 09 08 B5 B4 06 12 03 31 30 30
71 AB 00 00 01 D3 00 00 14 B7 00 00 02D4 08 94 30 6F
08 00 12 AF 03 08 99 C4 FF FF FF FF FF FF FF 01 10 D7 11 18 02 20 9C EC C1 91 F7 31 2A 85 03 08 F3 D3 0F 10 F4 03 18 00 22 D0 01 08
18 12 21 08 FF DA 97 EC 73 10 02 1A 12 E7 88 B1 E7 BB BF E8 89 B2 E7 9A 84 E6 9D BF E6 A0 97 20 BF E6 BD 01
1A([0-9a-fA-F]{2}|[0-9a-fA-F]{2}0[0-9a-fA-F])08([0-9a-fA-F]{12})12([0-9a-fA-F]{2})((?:[0-9a-fA-F]{2})+?)18([0-9a-fA-F]{2}|[0-9a-fA-F]{2}0[0-9a-fA-F])22([0-9a-fA-F]{2})((3[0-9])+?)28
				2 ID						4 名字                                                       7妖力
1A 3C 08 BD B1 A7 C2 C0 5A 12 12 E4 B9 90 E6 AD A3 E9 A3 9E E4 BA 8C E5 A4 A7 E7 8E 8B 18 A0 01 22 08 31 32 36 35 32 38 33 38 28 AA E5 08 30 94 4E 38 BF E6 BD 01 40 D1 FA 06 48 00 50 00
1A 36 08 BC B5 B4 C2 C0 5A 12 0C E8 94 9A E5 9C B0 E5 A4 A7 E4 BB 99                   18 A5 01 22 08 31 33 39 34 38 39 36 37 28 9F E5 08 30 94 4E 38 BF E6 BD 01 40 D1 FA 06 48 00 50 00
1A 33 08 81 BA B1 C2 C0 5A 12 0C E4 BD 98 E8 82 86 E5 B0 8A E8 80 85                   18 2E    22 06 37 34 38 31 37 39       28 AA E5 08 30 91 4E 38 BF E6 BD 01 40 D3 FA 06 48 00 50 00
28 C7 9E C8 01 3A 06 08 01 10 F9 90 02 3A 06 08 02 10 F3 D3 0F 3A 05 08 03 10 A8 2E 3A 05 08 04 10 B9 11 3A 05 08 05 10 D9 01 3A 05 08 06 10 E4 02 3A 05 08 07 10 DE 03 3A 05 08 08 10 DD 02 3A 05 08 09 10 95 02 3A 05 08 0A 10 8D 01 3A 05 08 0B 10 8F 03 3A 05 08 0C 10 C6 02 3A 05 08 0D 10 AD 03 3A 05 08 0E 10 88 05 3A 05 08 0F 10 BB 02 3A 05 08 10 10 EF 02 3A 04 08 17 10 3C 3A 04 08 19 10 0F 3A 04 08 1A 10 0B 3A 04 08 1B 100B 40 FF FF FF FF FF FF FF FF FF 01 50 00 58 AA E5 08 60 93 4E 68 D1 FA 06 70 00 30 00 38 BA C7 9A EC 73 48 0F 50 00 58 00 60 03 22 0B 0A 09 08 B5 B4 06 12 03 31 30 30

**/

//妖盟乱斗匹配结果
func ParaUnionBattleMatchInfo(dataBytes []byte) D.UnionBattleMatchInfo {
	var allInfo D.UnionBattleMatchInfo
	data := BytesToHexStr(dataBytes)
	if len(dataBytes) == 3 {
		allInfo.AlreadyMatch = true
	}
	//匹配自身妖力
	re := regexp.MustCompile(`00500028(([0-9a-fA-F]{2})+?)(3A|32)(0[0-9a-fA-F])08`)
	regRes := re.FindAllStringSubmatch(data, -1)
	if len(regRes) == 0 {
		log.Warnf("解析数据失败-%s", data)
	}
	if len(regRes) > 0 && len(regRes[0]) > 3 {
		//log.Infof("SelfCombatValue hex %s", regRes[0][1])
		allInfo.SelfCombatValue = ParaIntDataByHexStr(regRes[0][1])
	}
	//匹配妖盟信息
	re = regexp.MustCompile(`12([0-9a-fA-F]{2}|[0-9a-fA-F]{2}0[0-9a-fA-F])08([0-9a-fA-F]{10})10([0-9a-fA-F]{2,6})1A([0-9a-fA-F]{2})(([0-9a-fA-F]{2})+?)20`)
	regRes = re.FindAllStringSubmatch(data, -1)
	if len(regRes) == 0 {
		log.Warnf("解析数据失败-%s", data)
	}
	if len(regRes) > 0 && len(regRes[0]) > 3 {
		allInfo.ID = regRes[0][2]
		nameByte, _ := HexStrToBytes(regRes[0][5])
		allInfo.Name = string(nameByte) //妖盟名字
	}
	// 定义正则表达式
	re = regexp.MustCompile(`1A([0-9a-fA-F]{2}|[0-9a-fA-F]{2}0[0-9a-fA-F])08([0-9a-fA-F]{12})12([0-9a-fA-F]{2})((?:[0-9a-fA-F]{2})+?)18([0-9a-fA-F]{2}|[0-9a-fA-F]{2}0[0-9a-fA-F])22([0-9a-fA-F]{2})((3[0-9])+?)28`)
	res := re.FindAllStringIndex(data, -1)
	if len(res) == 0 {
		log.Errorf("解析数据失败-%s", data)
	}
	regRes = re.FindAllStringSubmatch(data, -1)
	//log.Debug("indices len : ", len(indices))
	// 打印结果
	for num, idx := range res {
		if idx[0]%2 != 0 || idx[1]%2 != 0 {
			continue
		}
		//startIdx := int(idx[0] / 2)
		//dataLen := int(ParaIntDataByHexStr(regRes[num][1])) + len(regRes[num][1])/2 + 1
		//endIdx := int(idx[1] / 2)
		//log.Debugf("%d-%s", dataLen, regRes[num][1:])
		//log.Debugf("%s", HexToHexStr(dataBytes[startIdx:startIdx+dataLen]))
		info := D.UnionBattlePeopleInfo{}
		info.ID = regRes[num][2]
		nameByte, _ := HexStrToBytes(regRes[num][4])
		info.Name = string(nameByte) //名字
		allCombatValueByte, _ := HexStrToBytes(regRes[num][7])
		allCombatValueStr := string(allCombatValueByte)
		allCombatValue, _ := strconv.ParseInt(allCombatValueStr, 10, 64)
		info.CombatValue = allCombatValue //总妖力
		//log.Infof("%+v", info)
		allInfo.PeopleInfo = append(allInfo.PeopleInfo, info)
	}
	sort.Sort(D.UnionBattlePeopleInfoByCombatValue(allInfo.PeopleInfo))
	if strings.Contains(data, "40FFFFFFFFFFFFFFFFFF") {
		allInfo.NotBuy = true
	}
	log.Infof("%+v", allInfo)
	return allInfo
}
